package com.example.util;

import java.io.*;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.text.DecimalFormat;

public class IOUtil {

    public static String getUserHomeResourcePath(String resName) {
        String p = System.getProperty("user.dir")+ File.separator+resName;
        return p;
    }

    /**
     * 获取当前类路径下的resName资源的完整路径
     * url.getPath()获取到的路径被utf-8编码了
     * 需要用URLDecoder.decode(path,"UTF-8")解码
     *
     * @param resName 需要获取完整路径的资源需要以/打头
     * @return 完整路径
     */
    public static String getResourcePath(String resName) {
        URL url = IOUtil.class.getResource(resName);
        String path = null;
        if(null == url){
            path = IOUtil.class.getResource("/").getPath()+resName;
        }else{
            path = url.getPath();
        }
        String decodePath = null;
        try {
            decodePath = URLDecoder.decode(path,"UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        if(JvmUtil.isWin()){
            return decodePath.substring(1);
        }
        return decodePath;
    }

    public static String buildResourcePath(String resName){
        URL url = IOUtil.class.getResource("/");
        String path = url.getPath();
        String decodePath = null;
        try {
            decodePath = URLDecoder.decode(path,"UTF-8");
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        return decodePath + resName;
    }

    public static void closeQuietly(java.io.Closeable o) {
        if (null == o) return;
        try {
            o.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getFormatFileSize(long length) {
        double size = ((double) length)/(1<<30);
        if(size >= 1){
            return fileSizeFormater.format(size)+"GB";
        }
        size = ((double) length)/(1<<20);
        if(size >= 1){
            return fileSizeFormater.format(size)+ "MB";
        }
        size = ((double) length)/(1<<20);
        if(size >= 1){
            return fileSizeFormater.format(size)+ "KB";
        }
        return length + "B";
    }

    private static DecimalFormat fileSizeFormater = FormatUtil.decimalFormat(1);

    public static String getVarStrFromBuf(ByteBuffer buf,int MaxLen){
        byte[] btemp = new byte[MaxLen];
        byte j = -1;
        int avaibleBytes = buf.array().length - buf.position();
        if(!(avaibleBytes > 0)){
            return "";
        }
        int len = 0;
        for (int i = 0;i < MaxLen;i++){
            btemp[i] = buf.get();
            len  = i;
            if(btemp[i] == 0){
                break;
            }
        }
        try {
            return new String(btemp,0,len,"UTF-8");
        }catch (UnsupportedEncodingException e){
            e.printStackTrace();
        }
        return null;
    }

    public static String toString(InputStream in,String s){
        return null;
    }

    public static String loadResourceFile(String resourceName){
        return loadJarFile(IOUtil.class.getClassLoader(),resourceName);
    }

    public static String loadJarFile(ClassLoader loader,String resourceName){
        InputStream in = loader.getResourceAsStream(resourceName);
        if(null == in){
            return null;
        }
        String out = null;
        try {
            int len = in.available();
            byte[] bytes = new byte[len];

            int readLength = in.read(bytes);
            if((long) readLength < len){
                throw new IOException("file length error:"+len);
            }
            out = new String(bytes, Charset.forName("UTF-8"));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeQuietly(in);
        }
        return out;
    }

    public static byte[] readInputStream(InputStream inputStream){
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int length;
        try {
            while ((length = inputStream.read(buffer))!=-1){
                outStream.write(buffer, 0, length);
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        return outStream.toByteArray();
    }

}
